package com.jike.noobmoney.mvp.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jike.noobmoney.R;

/**
 * Created by kekex on 2018/9/11.
 * 拍照的弹框(底部弹出)
 */

public class BottomDialog extends DialogFragment implements View.OnClickListener {

    private Context context;
    private Dialog dialog;
    private View view;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    public static BottomDialog getInstance() {
        return new BottomDialog();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new Dialog(context, R.style.Bottomdialog);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom, null);
        TextView tvItem1 = (TextView) view.findViewById(R.id.tv_item1);
        TextView tvItem2 = (TextView) view.findViewById(R.id.tv_item2);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);

        tvItem1.setOnClickListener(this);
        tvItem2.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);

        //设置取消标题
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置点击外部不可以消失
        dialog.setCanceledOnTouchOutside(false);
        //设置即使点击返回键也不会退出
        setCancelable(true);
        dialog.setContentView(view);


        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        //设置Dialog宽度匹配屏幕宽度
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置Dialog高度自适应
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    public OnClickCallback clickCallback;

    public interface OnClickCallback {

        void item1Click();

        void item2Click();

        void cancel();
    }

    public void setClickCallback(OnClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_item1) {
            //item1
            if (null != clickCallback) {
                clickCallback.item1Click();
            }
        } else if (id == R.id.tv_item2) {
            //item2
            if (null != clickCallback) {
                clickCallback.item2Click();
            }
        } else if (id == R.id.tv_cancel) {
            //取消
            if (null != clickCallback) {
                clickCallback.cancel();
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }


}
